const app = getApp()
import api from "../../utils/until"
const db = wx.cloud.database()
Page({

  data: {
    newsList: [{
      "title": "声明",
      "content": "本程序由个人开发、与学校或任何部门、组织无关。\n「课表」使用了学校公开的课程数据，进行整理并展示。"
    }],
    //开学周期 week周 day星期
    Today: {
      week: '',
      //今日时间（2020/4/11 星期六）
      theDay: '',
      day: ''
    },
    //学号
    xh: '',
    //今日课程
    course: "",
    DataList: "",
    help: ""
  },

  onLoad: function () {
    let xh = wx.getStorageSync('xh') ? wx.getStorageSync('xh') : '233';
    let help = wx.getStorageSync('help') ? wx.getStorageSync('help') : 'none';

    const theDay = api.getTheday()
    const week = api.getWeek()
    this.setData({
      xh,
      help,
      Today: {
        week: week,
        theDay: theDay
      }
    })
    this.getNotice();
    if (xh === '233') {
      console.log('不正确的学号')
    } else {
      this.key = this.data.xh + 'once' + this.data.Today.week.week
      app.globalData.xh = this.data.xh
      app.globalData.key = this.key
      app.globalData.week = week.week

      const temp = wx.getStorageSync(this.key)
      if (temp) {
        this.data.DataList = temp
        this.curr(this)
      } else {
        this.getToday(this.xh);
      }
    }

  },
  // 下拉刷新事件
  onPullDownRefresh: function () {
    wx.reLaunch({
      url: 'index'
    });
  },
  // 分享事件
  onShareAppMessage: function () {
    return {
      title: '我正在使用',
      desc: '「工程课表」,提供工程学院在校师生课表等查询服务。',
      path: '/pages/index/index'
    };
  },
  Tocour: function () {
    if (this.data.xh.length < 9) {
      wx.showToast({
        icon: 'none',
        title: '请设置正确的学号'
      })
    } else {
      wx.navigateTo({
        url: '/pages/core/table/index'
      });
    }
  },
  getToday: function (xh) {
    console.log('请求');
    wx.cloud.callFunction({
      // 要调用的云函数名称
      name: 'getCoursebyID',
      // 传递给云函数的event参数
      data: {
        'xh': app.globalData.xh,
        'week': app.globalData.week
      }
    }).then(res => {
      this.data.DataList = JSON.parse(res.result).content.list;
      console.log(this.data.DataList)
      if (this.data.DataList.length == 0) {
        console.log('xxxx')
      }
      wx.setStorageSync(this.key, this.data.DataList)
      this.curr(this)
    }).catch(err => {
      console.log(err)
    })


  },
  getNotice:function(){
    // 查询当前用户所有的 counters
    db.collection('Notice').get({
      success: res => {
        this.setData({
          newsList: res.data
        })
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询Notice记录失败'
        })
      }
    })
  },
  curr: function (mContext) {
    let result = mContext.data.DataList.filter(function (item) {
      return item.weekDay === mContext.data.Today.week.currday+''
    });
    this.setData({
      course:result
    })
  },
  toHelp: function () {
    wx.setStorageSync('help', '1');
    wx.navigateTo({
      url: '../more/help'
    });
  },
  setxh: function () {
    wx.redirectTo({
      url: '../core/set/today'
    });
  }
})
